package com.yhp.dao.impl;

import com.yhp.dao.BaseAdminDao;
import com.yhp.util.Dbutils;


import java.sql.*;
import java.util.Date;

public class AdminDaoImpl extends Dbutils implements BaseAdminDao {
    private static final String SQL_UPDATE_LOGIN_TIME = "UPDATE EADMIN SET LOGINTIME=?,LOGINIP=? WHERE USERNAME=?";
    private static final String SQL_LOGIN_ADMIN = "SELECT * FROM EADMIN WHERE USERNAME=? AND PASSWORD=?";


    /**
     * 修改登陆时间和IP地址
     *
     * @param username
     * @param date
     * @param ip
     *
     */
    @Override
    public void updateLoginTime(String username, Date date, String ip) {
        Connection connection = Dbutils.getConnection();
        PreparedStatement stat = null;
        try {
            stat = connection.prepareStatement(SQL_UPDATE_LOGIN_TIME);
            stat.setDate(1, (new java.sql.Date(date.getTime())));
            stat.setString(2,ip);
            stat.setString(3,username);
            int i = stat.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeall();
        }

    }

    /**
     * 管理员登陆
     *
     * @param username
     * @param password
     * @return 返回true登陆成功    返回false登陆失败
     */
    @Override
    public boolean loginAdmin(String username, String password) {
        Connection connection = Dbutils.getConnection();
        PreparedStatement stat = null;
        ResultSet resultSet = null;
        try {
            stat = connection.prepareStatement(SQL_LOGIN_ADMIN);
            stat.setString(1,username);
            stat.setString(2,password);
            resultSet = stat.executeQuery();
            return resultSet.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
           closeall();
        }
        return false;
    }
}
